package com.zjdiepu.www.biz.zx;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.zx.ZxQueryCreditApplySubMapper;
import com.zjdiepu.www.ibiz.zx.IZxQueryCreditApplySubService;
import com.zjdiepu.www.model.base.ext.ClientTypeExt;
import com.zjdiepu.www.model.base.vo.BusinessTotalVo;
import com.zjdiepu.www.model.zx.dto.ZxQueryCreditApplySub;
import com.zjdiepu.www.model.zx.ext.ZxQueryCreditApplySubExt;

/**
 * zx_征信查询申请子表-存储配偶及担保人信息Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年05月11日 16时15分<br/>
 * @since 1.0<br/>
 */
@Service
public class ZxQueryCreditApplySubServiceImpl extends BaseService implements IZxQueryCreditApplySubService {
	public static final Logger logger = LoggerFactory.getLogger(ZxQueryCreditApplySubServiceImpl.class);

	@Autowired
	private ZxQueryCreditApplySubMapper zxQueryCreditApplySubMapper;
	
	@Override
	public void saveUpdateAndDelete(ZxQueryCreditApplySub zxQueryCreditApplySub, OperateType operType) {
		switch (operType) {
		case add:
			zxQueryCreditApplySub.initInsertData(zxQueryCreditApplySub.getModifier(), zxQueryCreditApplySub.getModifierName());
			zxQueryCreditApplySubMapper.insert(zxQueryCreditApplySub);
			break;
		case edit:
			zxQueryCreditApplySub.initUpdateData(zxQueryCreditApplySub.getModifier(), zxQueryCreditApplySub.getModifierName());
			// 影响的记录数
			int updateCount = zxQueryCreditApplySubMapper.updateByPrimaryKeySelective(zxQueryCreditApplySub);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			zxQueryCreditApplySub.initDeleteData(zxQueryCreditApplySub.getModifier(), zxQueryCreditApplySub.getModifierName());
			// 影响的记录数
			int deleteCount = zxQueryCreditApplySubMapper.updateByPrimaryKeySelective(zxQueryCreditApplySub);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<ZxQueryCreditApplySub> selectByExt(ZxQueryCreditApplySubExt zxQueryCreditApplySubExt) {
		return zxQueryCreditApplySubMapper.selectByExt(zxQueryCreditApplySubExt);
	}
	
	@Override
	public int countByExt(ZxQueryCreditApplySubExt zxQueryCreditApplySubExt){
		return zxQueryCreditApplySubMapper.countByExt(zxQueryCreditApplySubExt);
	}
	
	@Override
	public ZxQueryCreditApplySub selectById(String id) {
		return zxQueryCreditApplySubMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			zxQueryCreditApplySubMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(ZxQueryCreditApplySub zxQueryCreditApplySub) {
		return zxQueryCreditApplySubMapper.countRefined(zxQueryCreditApplySub);
	}

	@Override
	public ZxQueryCreditApplySub selectRefined(ZxQueryCreditApplySub zxQueryCreditApplySub) {
		return zxQueryCreditApplySubMapper.selectRefined(zxQueryCreditApplySub);
	}
	
	@Override
	public int deletedRefined(ZxQueryCreditApplySub zxQueryCreditApplySub) {
		return zxQueryCreditApplySubMapper.deletedRefined(zxQueryCreditApplySub);
	}

	@Override
	public boolean repeat(ZxQueryCreditApplySub zxQueryCreditApplySub) {
		int count = countRefined(zxQueryCreditApplySub);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(zxQueryCreditApplySub.getId())) {
			return true;
		}
		ZxQueryCreditApplySub dbItem = selectRefined(zxQueryCreditApplySub);
		if(null == dbItem) {
			return false;
		}
		return !zxQueryCreditApplySub.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return zxQueryCreditApplySubMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<ZxQueryCreditApplySub> zxQueryCreditApplySubs) {
		return zxQueryCreditApplySubMapper.insertBatch(zxQueryCreditApplySubs);
	}

	@Override
	public int countZXNum(ClientTypeExt clientTypeExt) {
		return zxQueryCreditApplySubMapper.countZXNum(clientTypeExt);
	}

	@Override
	public List<BusinessTotalVo> selectZXNum(ClientTypeExt clientTypeExt) {
		return zxQueryCreditApplySubMapper.selectZXNum(clientTypeExt);
	}

	@Override
	public int countAllZXNum(ClientTypeExt clientTypeExt) {
		return zxQueryCreditApplySubMapper.countAllZXNum(clientTypeExt);
	}
}
